package components;

import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 * 系统托盘类
 * @author cccssw
 * 
 */
public class SysTray {
    public static String onService="out";
    public void show() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
        }

        /** 判断本地系统是否支持托盘 */
        if (SystemTray.isSupported()) {
            this.tray();
        } else {
            JOptionPane.showMessageDialog(null, "System has no stray support");
        }
    }

    /**  托盘相关代码 	 */
    private void tray() {

        /**  获得本操作系统托盘的实例  */
        tray = SystemTray.getSystemTray();
        String os = System.getProperty( "os.name","windows"); 
            System.out.println(os);
             URL imageUrl =null;
            if(isMac(os)){
                imageUrl = SysTray.class.getResource("mac_"+onService+".png");
                System.out.println("is_mac");
            }else if(isWindows(os)){
                imageUrl = SysTray.class.getResource("win_"+onService+".png");
                System.out.println("is_win");
            }else{
                imageUrl = SysTray.class.getResource("linux_"+onService+".png");
                System.out.println("is_nix");
            } 
            /** 设置托盘图标 */
            icon = new ImageIcon(imageUrl);

        /** 构造右键弹出式菜单 */
        PopupMenu pop = new PopupMenu(); // 构造一个右键弹出式菜单 

        /** 右键弹出式菜单的选项 */
        MenuItem about = new MenuItem("About");
        MenuItem exit = new MenuItem("Exit");



        pop.add(about);
        pop.add(exit);
        
        trayIcon = new TrayIcon(icon.getImage(), "JBlue running", pop);

        try {
            /** 将托盘图标添加到系统的托盘实例中 */
            tray.add(trayIcon); // 
        } catch (Exception e) {
        }
        
        /** 监听鼠标事件 */
        trayIcon.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    JOptionPane.showMessageDialog(null, "cccssw", "about", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

        /** 退出菜单项事件 */
        exit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    //bluetooth.getBluetooth().closeServer();					

                    tray.remove(trayIcon);
                    tray = null;

                    System.gc();

                    System.exit(0);
                } catch (Exception ex) {
                }
            }
        });

        /** 关于菜单项事件 */
        about.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Copyright @2011 \nAll Right Reserved\nauthor cccssw lvwenhan\nversion 2.0\nImproved by wssccc", "about", JOptionPane.INFORMATION_MESSAGE);
            }
        });
    }
    
    public static boolean isWindows(String os){ 
        return (os.indexOf( "dows" ) >= 0); //windows
    }
 
    public static boolean isMac(String os){
		//Mac
        return (os.indexOf( "ac" ) >= 0); 
    }
 
    public static boolean isUnix(String os){
		//linux or unix
        return (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0);
    }
    
    public static void  freshIcon(){
            String os = System.getProperty( "os.name","windows"); 
            System.out.println(os);
             URL imageUrl =null;
            if(isMac(os)){
                imageUrl = SysTray.class.getResource("mac_"+onService+".png");
                System.out.println("is_mac");
            }else if(isWindows(os)){
                imageUrl = SysTray.class.getResource("win_"+onService+".png");
                System.out.println("is_win");
            }else{
                imageUrl = SysTray.class.getResource("linux_"+onService+".png");
                System.out.println("is_nix");
            } 
            /** 设置托盘图标 */
            icon = new ImageIcon(imageUrl);
            trayIcon.setImage(icon.getImage());
            trayIcon.setImageAutoSize(true);
            System.out.println("image:"+imageUrl);
    }
    
    
    
    /** 托盘图标 */
    private static TrayIcon trayIcon = null;
    /** 本操作系统托盘的实例  */
    private SystemTray tray = null;
    /*系统托盘图标*/
    private static ImageIcon icon = null;
     
}
